package com.ruoyi.system.service.impl;

import com.ruoyi.system.entity.OneElectronFenceAlarm;
import com.ruoyi.system.mapper.OneElectronFenceAlarmMapper;
import com.ruoyi.system.service.OneElectronFenceAlarmService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 一号库区电子围栏报警信息(OneElectronFenceAlarm)表服务实现类
 *
 * @author makejava
 * @since 2023-10-25 13:43:09
 */
@Service("oneElectronFenceAlarmService")
public class OneElectronFenceAlarmServiceImpl implements OneElectronFenceAlarmService {
    @Resource
    private OneElectronFenceAlarmMapper oneElectronFenceAlarmDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OneElectronFenceAlarm queryById(Integer id) {
        return this.oneElectronFenceAlarmDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param oneElectronFenceAlarm 筛选条件
     * @param pageRequest           分页对象
     * @return 查询结果
     */
    @Override
    public Page<OneElectronFenceAlarm> queryByPage(OneElectronFenceAlarm oneElectronFenceAlarm, PageRequest pageRequest) {
        long total = this.oneElectronFenceAlarmDao.count(oneElectronFenceAlarm);
        return new PageImpl<>(this.oneElectronFenceAlarmDao.queryAllByLimit(oneElectronFenceAlarm, pageRequest), pageRequest, total);
    }

    @Override
    public List<OneElectronFenceAlarm> queryAll(OneElectronFenceAlarm oneElectronFenceAlarm) {
        List<OneElectronFenceAlarm> oneElectronFenceAlarms = oneElectronFenceAlarmDao.queryAll(oneElectronFenceAlarm);
        if (!CollectionUtils.isEmpty(oneElectronFenceAlarms)) {
            oneElectronFenceAlarms.forEach(var -> {
               // var.setDeviceId(String.format("%08d", Integer.valueOf(var.getDeviceId())));
            });
        }

        return oneElectronFenceAlarms;
    }

    /**
     * 新增数据
     *
     * @param oneElectronFenceAlarm 实例对象
     * @return 实例对象
     */
    @Override
    public OneElectronFenceAlarm insert(OneElectronFenceAlarm oneElectronFenceAlarm) {
        this.oneElectronFenceAlarmDao.insert(oneElectronFenceAlarm);
        return oneElectronFenceAlarm;
    }

    /**
     * 修改数据
     *
     * @param oneElectronFenceAlarm 实例对象
     * @return 实例对象
     */
    @Override
    public OneElectronFenceAlarm update(OneElectronFenceAlarm oneElectronFenceAlarm) {
        this.oneElectronFenceAlarmDao.update(oneElectronFenceAlarm);
        return this.queryById(oneElectronFenceAlarm.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.oneElectronFenceAlarmDao.deleteById(id) > 0;
    }
}
